﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace DSServer
{
    public class CClientHandler
    {
        CClient client;
        StreamReader sr;
        bool connected;

        public CClientHandler(CClient client)
        {
            this.client = client;
            Thread thread = new Thread(new ThreadStart(Init));
            thread.Start();
        }

        private void Init()
        {
            sr = new StreamReader(client.socket.GetStream());

            connected = true;

            Thread thread = new Thread(new ThreadStart(Run));
            thread.Start();
        }
        private void Run()
        {
            try
            {
                string msg = "";
                while (connected)
                {
                    msg = sr.ReadLine();
                    if (msg != null)
                    {
                        //Console.WriteLine("<< " + msg);
                        HandleMessage(msg);
                    }
                }

                //Console.WriteLine("Client " + client.username + " disconnected.");
                Program.DropClient(client.username);
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR (Run): " + e.Message);
            }
        }
        private void HandleMessage(string msg)
        {
            string[] tokens = msg.Trim().Split(' ');

            switch (tokens[0])
            {
                case "{CONNECT}":
                    // Check if the username is already in use
                    bool inUse = false;
                    foreach (CClient cl in Program.clients)
                    {
                        if (cl.username == tokens[1])
                        {
                            inUse = true;
                            break;
                        }
                    }
                    if (inUse)
                    {
                        Program.SendToUser(client, "{CONNECT_FAIL} Username is already in use.");
                        connected = false;
                    }
                    else
                    {
                        client.username = tokens[1];
                        client.password = tokens[2];
                        //Console.WriteLine("Client connected: " + client.username);

                        Program.SendToUser(client, "{CONNECT_OK} " + client.username);
                    }
                    break;

                case "{DISCONNECT}":
                    Program.SendToUser(client, "Client disconnected: " + client.username);
                    connected = false;
                    break;

                case "{CLIENTS}":
                    Program.SendToUser(client, "{CLIENTS} " + Program.GetClientList());
                    break;

                case "{MESSAGE}":
                    string minusFirst = msg.Substring(tokens[0].Length + 1);
                    Program.SendToAll("{MESSAGE} " + client.username + ": " + minusFirst);
                    break;
            }
        }
    }
}
